from tkinter import *
from tkinter import ttk

def createButton(frame, label):
    button = Button(frame, text=label, width=15, height=1)
    return button

def createSelectInput(frame, value, width=None):
    comvalue = StringVar()  # 窗体自带的文本，新建一个值
    comboxlist = ttk.Combobox(frame, textvariable=comvalue, width=width)  # 初始化
    comboxlist['state'] = 'readonly'
    comboxlist["values"] = value
    return comboxlist


def createCheckButton(frame, label, value, width=None, outside=None):
    def go(*args):
        if outside:
            outside(checkvar.get())
    checkvar = IntVar()  # 加粗-，斜体-，下划线-,在相同样式的段落间不添加空格-,保留原有特殊格式-
    return Checkbutton(frame, text=label, variable=checkvar, onvalue=value, offvalue=0, height=1, width=width, command=go)


def createSelectPlus(frame, go):
    box = Spinbox(frame, from_=0, to=999, increment=0.1, width=10, command=go)
    return box


def selectWithUnit(frame, value, outside):
    def go(*args):
        outside([number.get(), comlist.get()])
    pane = Frame(frame)
    number = createSelectPlus(pane, go)
    number.grid(row=0, column=0, padx=3, pady=3)
    comlist = createSelectInput(pane, value, width=5)
    comlist.grid(row=0, column=1, padx=3, pady=3)
    comlist.current(0)
    comlist.bind("<<ComboboxSelected>>", go)
    return pane


def setSWUValue(frame, ary):
    number = frame.children['!spinbox']
    comlist = frame.children['!combobox']
    if ary == None:
        number['value'] = 0.0
        comlist.current(0)
        return
    if ary[0] == None:
        ary[0] = 0.0
    number['value'] = ary[0]
    if ary[1] == None:
        comlist.current(0)
    else:
        comlist.current(comlist['values'].index(ary[1]))


def setShowValue(jsonData, main):
    i = jsonData
    self = main
    if i.get('Font'):
        self.vFontList.current(self.vFontList['values'].index(i['Font']))
    else:
        self.vFontList.set('')
    if i.get('FontSize'):
        self.vFontSizeList.current(
            self.vFontSizeList['values'].index(i['FontSize']))
    else:
        self.vFontSizeList.set('')
    if i.get('SymbolType'):
        self.vSymbolTypeList.current(
            self.vSymbolTypeList['values'].index(i['SymbolType']))
    else:
        self.vSymbolTypeList.set('')
    if i.get('Alignment'):
        self.vAlignmentList.current(
            self.vAlignmentList['values'].index(i['Alignment']))
    else:
        self.vAlignmentList.set('')
    if i.get('SpecialFormat'):
        self.vSpecialFormatList.current(
            self.vSpecialFormatList['values'].index(i['SpecialFormat']))
    else:
        self.vSpecialFormatList.set('')
    if i.get('LineSpacingType'):
        self.vLineSpacingTypeList.current(
            self.vLineSpacingTypeList['values'].index(i['LineSpacingType']))
    else:
        self.vLineSpacingTypeList.set('')
    if i.get('在相同样式的段落间不添加空格'):
        self.v4List.select()
    else:
        self.v4List.deselect()
    if i.get('保留原有特殊格式'):
        self.v5List.select()
    else:
        self.v5List.deselect()
    if i.get('bold'):
        self.boldCheckButton.select()
    else:
        self.boldCheckButton.deselect()
    if i.get('italic'):
        self.italicCheckButton.select()
    else:
        self.italicCheckButton.deselect()
    if i.get('underline'):
        self.underlineCheckButton.select()
    else:
        self.underlineCheckButton.deselect()
    # if i.get('leftPadding'):
    setSWUValue(self.leftPaddingList, i.get('leftPadding'))
    # if i.get('rightPadding'):
    setSWUValue(self.rightPaddingList, i.get('rightPadding'))
    # if i.get('indented'):
    setSWUValue(self.indentedList, i.get('indented'))
    # if i.get('beforeParagraph'):
    setSWUValue(self.beforePList, i.get('beforeParagraph'))
    # if i.get('afterParagraph'):
    setSWUValue(self.afterPList, i.get('afterParagraph'))
    # if i.get('lineSpacing'):
    setSWUValue(self.lineSpacingValueList, i.get('lineSpacing'))
